#include "audioplayer.h"

AudioPlayer::AudioPlayer(QObject *parent) :
    QObject(parent)
{
    m_audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
    m_mediaObject = new Phonon::MediaObject;
    m_mediaObject->setTransitionTime(-5000);
    Phonon::createPath(m_mediaObject, m_audioOutput);

    connect(m_mediaObject, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
            this, SLOT(stateChanged(Phonon::State,Phonon::State)));
    connect(m_mediaObject, SIGNAL(finished()),
            this, SLOT(finished()));

    m_volume = 1.0;
    m_volumeAnimation = new QPropertyAnimation(m_audioOutput, "volume");
    m_volumeAnimation->setDuration(3000);
    connect(m_volumeAnimation, SIGNAL(finished()),
            this, SLOT(volumeAnimationFinish()));

    m_ringEnable = false;
}

void AudioPlayer::clear()
{
    m_mediaObject->stop();
    m_mediaObject->clear();
    m_mediaQueue.clear();
}

void AudioPlayer::addMusic(QVector<Phonon::MediaSource> &media)
{
    m_mediaQueue = media;
    if (media.size() <= 0)
        return;
    m_mediaObject->setCurrentSource(media[0]);
    for (int i = 1; i < media.size(); ++i)
        m_mediaObject->enqueue(media[i]);
}

void AudioPlayer::play()
{
    m_mediaObject->play();
    volumeUp();
}

void AudioPlayer::pause()
{
    volumeDown();
}

void AudioPlayer::setVolume(qreal value)
{
    if (value == 0.0)
        qWarning() << "AudioPlayer::setVolume: Mute!";
    m_audioOutput->setVolume(value);
}

void AudioPlayer::setRingEnable(bool enable)
{
    m_ringEnable = enable;
}

void AudioPlayer::volumeUp()
{
    m_volumeAnimation->setStartValue(0.0);
    m_volumeAnimation->setEndValue(m_volume);
    m_volumeAnimation->start();
}

void AudioPlayer::volumeDown()
{
    m_volumeAnimation->setStartValue(m_volume);
    m_volumeAnimation->setEndValue(0.0);
    m_volumeAnimation->start();
}

void AudioPlayer::volumeAnimationFinish()
{
    if (m_audioOutput->volume() == 0.0)
        m_mediaObject->pause();
}

void AudioPlayer::finished()
{
    if (m_ringEnable) {
        addMusic(m_mediaQueue);
        m_mediaObject->play();
    }
}

void AudioPlayer::stateChanged(Phonon::State oldState, Phonon::State newState)
{
    Q_UNUSED(oldState)
    switch (newState) {
    case Phonon::ErrorState:
        if (m_mediaObject->errorType() == Phonon::FatalError) {
            qWarning() << "AudioPlayer: fatal" << m_mediaObject->errorString();
        } else {
            qDebug() << "AudioPlayer: non fatal" << m_mediaObject->errorString();
        }
        break;

    case Phonon::PlayingState:
        //qDebug() << "AudioPlayer: playing:" << m_mediaObject->currentSource().fileName();
        break;
    case Phonon::StoppedState:
        //qDebug() << "AudioPlayer: stopped";
        break;
    case Phonon::PausedState:
        break;
    case Phonon::BufferingState:
        break;
    default:
        ;
    }

    /*if (newState != Phonon::StoppedState && newState != Phonon::PausedState) { // play?
        qWarning() << "AudioPlayer:" << "? stopped || paused" << m_mediaObject->currentSource().fileName();
        return;
    }*/

    if (m_mediaObject->currentSource().type() == Phonon::MediaSource::Invalid) {
        qWarning() << "AudioPlayer:" << "invalid source:" << m_mediaObject->currentSource().fileName();
        return;
    }
}
